﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Cash5
{
    public partial class Form1 : Form
    {
        Lotto lt;
        PossibleNumbers ps;
        public Form1()
        {
            InitializeComponent();
            lt = Lotto.instance;
            initialize();
            initCalculator();
            updateLabels();
            lbCopirygh.Text = " 2012 © King of Cash 5. \r\n All Rights Reserved";
        }

        private void btShowAll_Click(object sender, EventArgs e)
        {
            //ShowAll all = new ShowAll(ps.allCombinationsAsString());
            //all.Text = "All Permutations";
            //all.ShowDialog();

            var all = new NewShowAll(ps.allCombinations);
            all.Text = "All Permutations";
            all.ShowDialog();
        }
        private void initialize()
        {
            int from = 0, to = 0, digits = 0;
            int.TryParse(txFrom.Text, out from);
            int.TryParse(txTo.Text, out to);
            int.TryParse(txDigits.Text, out digits);
            if (to - from > 50)
            {
               DialogResult res = MessageBox.Show("It is going to take a lot of time to calculate all possibilities. Are you sure that you want calculate them all?", 
                   "Oops",MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
               if (res == DialogResult.Cancel)
               {
                   txTo.Focus();
                   return;
               }
                
            }
            lt.From = from;
            lt.To = to;
            lt.Digits = digits;
      

        }
        private void initCalculator()
        {
                ps = new PossibleNumbers(lt);
        }
        private static List<int> parseExcludeNumbers(string numbers)
        {
            string[] sNumbers = Regex.Split(numbers, @"\s+");
            List<int> nums = new List<int>();
            for (int i = 0; i < sNumbers.Length; i++)
            {
                int result = 0;
                if (int.TryParse(sNumbers[i], out result))
                {
                    if (!nums.Contains(result))
                    {
                        nums.Add(result);
                    }
                }
            }
            return nums;
        }

        private static List<List<int>> parseNumbers(string numbers)
        {
            List<List<int>> nums = new List<List<int>>();
            if (String.Empty != numbers)
            {
                string[] rows = numbers.Split('\r', '\n');
                for (int k = 0; k < rows.Length; k++)
                {
                    if (String.Empty != rows[k].Trim())
                    {
                        string[] lines = Regex.Split(rows[k].Trim(), @"\s+");

                        for (int i = 0; i < lines.Length; i++)
                        {
                            if (String.Empty != lines[i].Trim())
                            {
                                string[] str = Regex.Split(lines[i], @"-");
                                List<int> tmp = new List<int>();
                                for (int j = 0; j < str.Length; j++)
                                {
                                    if (String.Empty != str[j].Trim())
                                        tmp.Add(int.Parse(str[j].Trim()));
                                }
                                nums.Add(tmp);
                            }
                        }
                    }
                }

            }
            return nums;
        }
        private void btShowRemaining_Click(object sender, EventArgs e)
        {
            //ShowAll all = new ShowAll(ps.combinationsAsString());
            //all.Text = "Remaining permutations";
            //all.ShowDialog();

            var all = new NewShowAll(ps.remainingCombinations);
            all.Text = "Remaining permutations";
            all.ShowDialog();
        }

        private void btClear_Click(object sender, EventArgs e)
        {
            txExclude.Text = "";
           // initialize();
           // updateLabels();
        }

        private void btCalculatePossibilities_Click(object sender, EventArgs e)
        {
            string numbers = txExclude.Text;
            List<int> nums = parseExcludeNumbers(numbers);
            if (nums.Count > 0)
            {
                lt.ExcludeNumbers = nums;
                try
                {
                    ps.recalculateWitSkipList();
                }
                catch (CustomExceptionton ex)
                {
                    MessageBox.Show(ex.Message, "Hint", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //ps.recalculateWitSkipList();

                }
            }

            updateLabels();

        }

        private void txFrom_TextChanged(object sender, EventArgs e)
        {
            initialize();
            initCalculator();
            updateLabels();
        }
        void updateLabels()
        {
           // lt.calculatePossibilites();
            lbPossibleCombinations.Text = lt.PossibleCombinations.ToString();
            //lt.recalculateRemaining();
            lbRemaining.Text = lt.RemainingCombinations.ToString();

        }

        private void txTo_TextChanged(object sender, EventArgs e)
        {
            initialize();
            initCalculator();
            updateLabels();
        }

        private void txDigits_TextChanged(object sender, EventArgs e)
        {
            initialize();
            initCalculator();
            updateLabels();
        }

        private void txExclude_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btReset_Click(object sender, EventArgs e)
        {
            ps.resetRemaining();
            updateLabels();
            
        }

        private void lbCopirygh_Click(object sender, EventArgs e)
        {

        }


    }
}
